package com.hnkywl.sts.service.tjbb;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.tjbb.KhwlDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-28
 * Time: 下午5:13
 * To change this template use File | Settings | File Templates.
 */
@Service
public class KhwlService {
    private KhwlDao khwlDao;

    /**
     * 客户往来集合
     *
     * @param gsid
     * @param deptid
     * @param enddt
     * @param custname
     * @param pageNo
     * @param pageSize
     * @param tc_flag
     * @return
     */
    public String KhwlList(Integer gsid, Integer deptid, Date enddt, String custname, Integer pageNo, Integer pageSize, Integer tc_flag) {
        Map<String, Object> result = khwlDao.khwlList(gsid, deptid, enddt, custname, pageNo, pageSize, tc_flag);
        Integer total = (Integer) result.get("total");
        List<Map> rows = (List<Map>) result.get("rows");
        List<Map> footer = (List<Map>) result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String footerJson = gson.toJson(footer);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson + ",\"total\":" + total + "}";
        return json;
    }

    /**
     * 客户往来对账
     *
     * @param gsid
     * @param deptid
     * @param begindt
     * @param enddt
     * @param custname
     * @param type
     * @return
     */
    public String KhwlDz(Integer gsid, Integer deptid, Date begindt, Date enddt, String custname, Integer type, Integer pageNo, Integer pageSize) {
        Map<String, Object> result = khwlDao.khwlDzList(gsid, deptid, begindt, enddt, custname, type, pageNo, pageSize);
        Integer total = (Integer) result.get("total");
        List<Map> rows = (List<Map>) result.get("rows");
        List<Map> footer = (List<Map>) result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String footerJson = gson.toJson(footer);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson + ",\"total\":" + total + "}";
        return json;
    }

    /**
     * 销售未结算金额
     *
     * @param gsid
     * @param enddt
     * @param custname
     * @return
     */
    public String xswjsje(Integer gsid, Date enddt, String custname) {
        List<Map> list = khwlDao.xswjsje(gsid, enddt, custname);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    @Autowired
    public void setKhwlDao(KhwlDao khwlDao) {
        this.khwlDao = khwlDao;
    }
}
